package cn.lastwhisper.leetcode.dynamic.完全平方数_279_中等;

import javafx.util.Pair;

import java.util.LinkedList;

/**
 * 题目地址：https://leetcode-cn.com/problems/perfect-squares/
 * -------------------------------------------------------------------
 * 思考：
 * -------------------------------------------------------------------
 * 思路：
 *  图，使用visited数组,记录每一个入队元素
 * -------------------------------------------------------------------
 * 时间复杂度：O(n)
 * 空间复杂度：O(n)
 */
public class Solution2 {

    public int numSquares(int n) {

        LinkedList<Pair<Integer, Integer>> queue = new LinkedList<Pair<Integer, Integer>>();
        queue.addLast(new Pair<Integer, Integer>(n, 0));

        boolean[] visited = new boolean[n + 1];
        visited[n] = true;

        while (!queue.isEmpty()) {
            Pair<Integer, Integer> front = queue.removeFirst();
            int num = front.getKey();
            int step = front.getValue();

            if (num == 0)
                return step;

            for (int i = 1; num - i * i >= 0; i++)
                if (!visited[num - i * i]) {
                    queue.addLast(new Pair<>(num - i * i, step + 1));
                    visited[num - i * i] = true;
                }
        }

        throw new IllegalStateException("No Solution.");
    }

    public static void main(String[] args) {

        System.out.println((new Solution2()).numSquares(12));
        System.out.println((new Solution2()).numSquares(13));
    }
}
